<template>
  <div class="app-container-flex">
    <NewFullScreen
      v-model="showDialog"
      :title="title"
      :btnList="fullInfo.btnList"
    >
      <div class="model-panel">
        <div class="model-panel-title">
          <p>{{ $t('basic_information') }}</p>
        </div>
        <div class="model-panel-body">
          <PageForm
            ref="pageFormRef"
            class="paddingBox order-model-formBox flex_wrap_start_start"
            v-model:ref-obj="formInfo.ref"
            :data="formInfo.data"
            :field-list="formInfo.fieldList"
            :label-width="formInfo.labelWidth"
            :rules="formInfo.rules"
          ></PageForm>
        </div>
      </div>
    </NewFullScreen>
  </div>
</template>

<script setup name="orgModify">
import { getPlatformApplicationInfo, updatePlatformApplicationInfo, auditPlatformApplicationInfo } from '@/api/base.js'
const emits = defineEmits()
const store = useStore()
const { proxy } = getCurrentInstance()
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '',
  },
  tableRow: {
    type: Object,
    default: () => {
      return {}
    },
  },
})
// 表单相关
const formInfo = ref({
  ref: null,
  data: {},
  fieldList: [
    {
      label: '名称',
      value: 'name',
      type: 'input',
      disabled:true
    },
    {
      label: '编码',
      value: 'orgCode',
      type: 'input',
      disabled:true
    },
    {
      label: '统一社会信用代码',
      value: 'uniformSocialCreditCode',
      type: 'input',
      maxlength: 20,
      disabled:true
    },
    {
      label: '机构类型',
      value: 'typeValue',
      type: 'input',
      disabled:true
    },
   
    {
      label: '服务范围',
      value: 'serviceScope',
      type: 'select-model',
      code: 'service_scope',
      clearable: true,
    }

  ],
  rules: {
    // serviceScope: [{ required: true, message: '请选择服务范围', trigger: 'change' }]
  },
  labelWidth: '120px',
})
const fullInfo = ref({
  btnList: [{ label: '确认', event: confirm, show: proxy.$permission('platformApplication:confirm') }, { label: '保存', event: save, show: true }],
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    emits('update:visible', val) // 更新父组件visible
  },
})

function save() {
  const _formInfo = formInfo.value
  proxy.$refs.pageFormRef.$refs.form.validate((valid) => {
    if (valid) {
      const params = _formInfo.data
      const api = updatePlatformApplicationInfo
      if(!params.serviceScope) {
        params.serviceScope = -2
        params.serviceScopeValue = ''
      }
      api(params).then((res) => {
        if (res.success) {
          showDialog.value = false
          emits('returnModify', false)
        }
      })
    }
  })
}
function confirm() {
  const _formInfo = formInfo.value
  proxy.$refs.pageFormRef.$refs.form.validate((valid) => {
    if (valid) {
      const params = _formInfo.data
      const api = auditPlatformApplicationInfo
      api(params).then((res) => {
        if (res.success) {
          showDialog.value = false
          emits('returnModify', false)
        }
      })
    }
  })
}

function getInfo(){
  getPlatformApplicationInfo(props.tableRow.id).then(res => {
    formInfo.value.data = res.data
  })
}

onMounted(() => {
  getInfo()
})
</script>

<style scoped></style>
